<?php
namespace Admin\Controller;

class PhotosController extends CommonController {

    public function _initialize() {
        parent::_initialize();
        $this->info['module_name_zh'] = '照片';
    }

    //照片列表
    public function index(){
        $where['status'] = '1';
        if(I('keywords')) $where['id'] = intval(I('keywords'));

        //排序
        $orderArr = array();
        if(I('num')) $orderArr[] = "num ".I('num');
        if(I('create_time')) $orderArr[] = "id ".I('create_time');
        $order = implode(',', $orderArr);
        if (empty($order)) $order = "num desc, id asc";

        $dataPage = D(CONTROLLER_NAME)->getPager($where, 5, $order);
        $list = $dataPage['data'];

        foreach ($list as $k => $v) {
            $list[$k]['index'] = $this->getRankNum($v);
            //用户信息，优先从微信用户表查，若无则从用户表查
            $wxMemberRow = M('WxMember')->where(array('fromuser' => $v['openid']))->find();
            if (empty($wxMemberRow['card_num'])) {
                $memberRow = M('Member')->where(array('openid' => $v['openid']))->find();
                $wxMemberRow['card_num']   = $memberRow['card_num'];
                $wxMemberRow['member_num'] = $memberRow['member_num'];
                $wxMemberRow['phone']      = $memberRow['phone'];
            }
            $list[$k]['WxMember'] = $wxMemberRow;
        }
        $this->info['list'] = $list;
        $this->info['sum']  = $dataPage['sum'];
        $this->info['page'] = $dataPage['page'];

        $this->assign($this->info);
        // dump($this->info);die;
        $this->display();
    }

    //计算照片排名
    public function getRankNum($photoRow) {
        $greaterNum = M('Photos')->where(array('num' => array('gt', $photoRow['num']), 'status' => '1'))->count();//大于该照片的数
        $eqNum = M('Photos')->where(array('num' => $photoRow['num'], 'id' => array('lt', $photoRow['id']), 'status' => '1'))->count();//等于但上传比该照片早的数
        $rankNum = $greaterNum + $eqNum + 1;
        return $rankNum;
    }

    /**
     * 修改赞数
     * 127.0.0.1/redcard/index.php?m=Admin&c=Photos&a=editNum&ajax=1&id=1&num=10
     */
    public function editNum() {
        $id  = I('id', 0 , 'intval');
        $num = I('num', 0 , 'intval');

        M('Photos')->where(array('id' => $id))->save(array('num' => $num));
        $this->success('修改成功');
    }

    /**
     * 状态删除
     * 127.0.0.1/redcard/index.php?m=Admin&c=Photos&a=del&ajax=1&id=1
     */
    public function del() {
        $id  = I('id', 0 , 'intval');

        M('Photos')->where(array('id' => $id))->save(array('status' => '3'));
        $this->success('删除成功');
    }

    //导出
    public function output() {
        set_time_limit(0);
        $filename = '小红卡活动排行榜';
        $title = '照片排行榜';
        $data = M('Photos')->where(array('status' => '1'))->order('num desc, id asc')->select();
        foreach ($data as $k => $v) {
            //用户信息，优先从微信用户表查，若无则从用户表查
            $wxMemberRow = M('WxMember')->where(array('fromuser' => $v['openid']))->find();
            if (empty($wxMemberRow['card_num'])) {
                $memberRow = M('Member')->where(array('openid' => $v['openid']))->find();
                $wxMemberRow['card_num']   = $memberRow['card_num'];
                $wxMemberRow['member_num'] = $memberRow['member_num'];
                $wxMemberRow['phone']      = $memberRow['phone'];
            }
            $data[$k]['WxMember'] = $wxMemberRow;
        }

        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A2', 'ID')
                    ->setCellValue('B2', '赞数')
                    ->setCellValue('C2', '昵称')
                    ->setCellValue('D2', '手机号')
                    ->setCellValue('E2', '卡号')
                    ->setCellValue('F2', '会员编号')
                    ->setCellValue('G2', '上传时间');

        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $title);//标题
        foreach($data as $lk=>$lv){
            $lj=$lk+3;
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $lj, $lv['id'])
                                          ->setCellValue('B' . $lj, $lv['num'])
                                          ->setCellValue('C' . $lj, $lv['WxMember']['nickname'])
                                          ->setCellValue('D' . $lj, $lv['WxMember']['phone'])
                                          ->setCellValue('E' . $lj, $lv['WxMember']['card_num'])
                                          ->setCellValue('F' . $lj, $lv['WxMember']['member_num'])
                                          ->setCellValue('G' . $lj, $lv['create_time']);
        }

        $objPHPExcel->getActiveSheet()->setTitle('数据');
        $objPHPExcel->setActiveSheetIndex(0);

        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }

}
